<?php
/**
 * 后台管理
 * @author jianhui
 *
 */
class Admin extends MY_Controller{
    function Admin(){
        parent::MY_Controller();
        $this->user['userRole'] == 6 || showError('您无法进入此页面');
        //设置导航
        $this->navbarList[] = array('Admin','index', 'title'=>'后台管理');
        //主题
        //$this->theme = 'bootstrap';
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('Admin', 'index'),'title'=>'管理首页');
        //标签菜单
        $this->navbar = array(
            array('title'=>'管理端首页','url'=>printUrl('Admin','index'),'focusTag'=>'index'),
            array('title'=>'时段配置','url'=>printUrl('Admin','timeUnitList'),'focusTag'=>'timeUnitList'),
            array('title'=>'巡展手动同步','url'=>printUrl('Admin','xunSync'),'focusTag'=>'xunSync'),
            array('title'=>'巡展同步数据日志','url'=>printUrl('Admin','xunLogDataList'),'focusTag'=>'xunLogDataList'),
            array('title'=>'巡展同步错误日志','url'=>printUrl('Admin','xunLogList'),'focusTag'=>'xunLogList'),
            array('title'=>'巡展订单数据导出','url'=>printUrl('Admin','xunOutput'),'focusTag'=>'xunOutput'),
            array('title'=>'BTL巡展订单数据检查','url'=>printUrl('Admin','xunDataCheck'),'focusTag'=>'xunDataCheck'),
            array('title'=>'数据重建','url'=>printUrl('Admin','rebuild'),'focusTag'=>'rebuild'),
        );
        $this->viewData['focusTag'] = $this->input->get('m') ? $this->input->get('m') : 'index';
    }
    /**
     * 首页
     */
    function index(){
        $this->load->view('Admin/index', $this->viewData);
    }
    /**
     * 时段列表
     */
    function timeUnitList(){
        //年份
        $this->viewData['yearList'] = $this->timeUnitModel->getCacheData('yearList');
        $where = array();
        //年份
        $year = $this->input->get('year');
        $this->viewData['year'] = $year;
        $where['year'] = $year ? $year : $this->config->item('nowYear');
        $this->viewData['timeUnitList'] = $this->timeUnitModel->getList($where, '', '', '', 'orderNum asc,id asc');
        $this->load->view('Admin/timeUnitList', $this->viewData);
    }
    /**
     * 增加或修改时段
     */
    function timeUnitAdd(){
        $this->theme = 'bootstrap';
        if ($_POST){
            $id = $this->input->post('id');
            $data = array(
                'year'  => (int)$this->input->post('year'),
                'name'  => $this->input->post('name'),
                'orderNum'  => (int)$this->input->post('orderNum'),
                'startTime' => $this->input->post('startTime'),
                'endTime'   => $this->input->post('endTime'),
            );
            //单店时间设置
            $singleTimePost = $this->input->post('singleTime');
            $singleTimeSetting = array(
                'budgetApplyStart'    => $singleTimePost['budgetApplyStart'],
                'budgetApplyEnd'      => $singleTimePost['budgetApplyEnd'],
                'budgetAuditStart'    => $singleTimePost['budgetAuditStart'],
                'budgetAuditEnd'      => $singleTimePost['budgetAuditEnd'],
                'settleApplyStart'    => $singleTimePost['settleApplyStart'],
                'settleApplyEnd'      => $singleTimePost['settleApplyEnd'],
                'settleAuditStart'    => $singleTimePost['settleAuditStart'],
                'settleAuditEnd'      => $singleTimePost['settleAuditEnd'],
                'settleScoreStart'    => $singleTimePost['settleScoreStart'],
                'settleScoreEnd'      => $singleTimePost['settleScoreEnd'],
                'settleChangeStart'   => $singleTimePost['settleChangeStart'],
                'settleChangeEnd'     => $singleTimePost['settleChangeEnd'],
            );
            //城市提升基金时间设置
            $cityTimePost = $this->input->post('cityTime');
            $cityTimeSetting = array(
                'budgetApplyStart'      => $cityTimePost['budgetApplyStart'],
                'budgetApplyEnd'        => $cityTimePost['budgetApplyEnd'],
                'budgetAuditStart'      => $cityTimePost['budgetAuditStart'],
                'budgetAuditEnd'        => $cityTimePost['budgetAuditEnd'],
                'settleApplyStart'      => $cityTimePost['settleApplyStart'],
                'settleApplyEnd'        => $cityTimePost['settleApplyEnd'],
                'settleAuditStart'      => $cityTimePost['settleAuditStart'],
                'settleAuditEnd'        => $cityTimePost['settleAuditEnd'],
                'settleScoreStart'      => $cityTimePost['settleScoreStart'],
                'settleScoreEnd'        => $cityTimePost['settleScoreEnd'],
                'settleChangeStart'     => $cityTimePost['settleChangeStart'],
                'settleChangeEnd'       => $cityTimePost['settleChangeEnd'],
            );
            //车展基金
            $carTimePost = $this->input->post('carTime');
            $carTimeSetting = array(
                'budgetApplyStart'      => $carTimePost['budgetApplyStart'],
                'budgetApplyEnd'        => $carTimePost['budgetApplyEnd'],
                'budgetAuditStart'      => $carTimePost['budgetAuditStart'],
                'budgetAuditEnd'        => $carTimePost['budgetAuditEnd'],
                'settleApplyStart'      => $carTimePost['settleApplyStart'],
                'settleApplyEnd'        => $carTimePost['settleApplyEnd'],
                'settleAuditStart'      => $carTimePost['settleAuditStart'],
                'settleAuditEnd'        => $carTimePost['settleAuditEnd'],
                'settleChangeStart'     => $carTimePost['settleChangeStart'],
                'settleChangeEnd'       => $carTimePost['settleChangeEnd'],
            );
            //巡展基金时间设置
            $xunTimePost = $this->input->post('xunTime');
            $xunTimeSetting = array(
                'settleAuditStart' => $xunTimePost['settleAuditStart'],
                'settleAuditEnd' => $xunTimePost['settleAuditEnd'],
            );
            //固定传播基金
            $spreadTimePost = $this->input->post('spreadTime');
            $spreadTimeSetting = array(
                'budgetApplyStart'      => $spreadTimePost['budgetApplyStart'],
                'budgetApplyEnd'        => $spreadTimePost['budgetApplyEnd'],
                'budgetAuditStart'      => $spreadTimePost['budgetAuditStart'],
                'budgetAuditEnd'        => $spreadTimePost['budgetAuditEnd'],
                'settleApplyStart'      => $spreadTimePost['settleApplyStart'],
                'settleApplyEnd'        => $spreadTimePost['settleApplyEnd'],
                'settleAuditStart'      => $spreadTimePost['settleAuditStart'],
                'settleAuditEnd'        => $spreadTimePost['settleAuditEnd'],
                'settleChangeStart'     => $spreadTimePost['settleChangeStart'],
                'settleChangeEnd'       => $spreadTimePost['settleChangeEnd'],
            );
            //专项单店基金
            $specialTimePost = $this->input->post('specialTime');
            $specialTimeSetting = array(
                'budgetApplyStart'      => $specialTimePost['budgetApplyStart'],
                'budgetApplyEnd'        => $specialTimePost['budgetApplyEnd'],
                'budgetAuditStart'      => $specialTimePost['budgetAuditStart'],
                'budgetAuditEnd'        => $specialTimePost['budgetAuditEnd'],
                'settleApplyStart'      => $specialTimePost['settleApplyStart'],
                'settleApplyEnd'        => $specialTimePost['settleApplyEnd'],
                'settleAuditStart'      => $specialTimePost['settleAuditStart'],
                'settleAuditEnd'        => $specialTimePost['settleAuditEnd'],
                'settleScoreStart'      => $specialTimePost['settleScoreStart'],
                'settleScoreEnd'        => $specialTimePost['settleScoreEnd'],
                'settleChangeStart'     => $specialTimePost['settleChangeStart'],
                'settleChangeEnd'       => $specialTimePost['settleChangeEnd'],
            );
            //专项城市基金
            $specityTimePost = $this->input->post('specityTime');
            $specityTimeSetting = array(
                'budgetApplyStart'      => $specityTimePost['budgetApplyStart'],
                'budgetApplyEnd'        => $specityTimePost['budgetApplyEnd'],
                'budgetAuditStart'      => $specityTimePost['budgetAuditStart'],
                'budgetAuditEnd'        => $specityTimePost['budgetAuditEnd'],
                'settleApplyStart'      => $specityTimePost['settleApplyStart'],
                'settleApplyEnd'        => $specityTimePost['settleApplyEnd'],
                'settleAuditStart'      => $specityTimePost['settleAuditStart'],
                'settleAuditEnd'        => $specityTimePost['settleAuditEnd'],
                'settleScoreStart'      => $specityTimePost['settleScoreStart'],
                'settleScoreEnd'        => $specityTimePost['settleScoreEnd'],
                'settleChangeStart'     => $specityTimePost['settleChangeStart'],
                'settleChangeEnd'       => $specityTimePost['settleChangeEnd'],
            );
            $data['year'] || showError('请输入年份');
            $data['year'] >= 2016 || showError('年份应大于或等于2016年');
            $data['name'] || showError('请输入时段名称');
            $data['orderNum'] || $data['orderNum'] = 0;
            $data['startTime'] || showError('请选择时段开始时间');
            $data['endTime'] || showError('请选择时段结束时间');
            date('d', strtotime($data['startTime'])) == 1 || showError('开始时间非当月第一天');
            //结束时间戳
            $endTime = strtotime($data['endTime']);
            date('d', $endTime) == date('t', $endTime) || showError('结束时间非当月最后一天');
            if ($this->timeUnitModel->getData(array('year'=>$data['year'],'name'=>$data['name'],'id !='=>$id))){
                showError('已存在同名时段');
            }
            //默认初始化时间
            /**
             * 单店基金
             */
            //单店基金预算申请开始时间默认为【时段开始前十天】
            $singleTimeSetting['budgetApplyStart'] || $singleTimeSetting['budgetApplyStart'] = date("Y-m-d H:i:s", strtotime($data['startTime'].' -10 day'));
            //单店基金预算申请结束时间默认为【时段结束时间】            
            $singleTimeSetting['budgetApplyEnd'] || $singleTimeSetting['budgetApplyEnd'] = $data['endTime'];
            //单店基金预算审核开始时间默认为【预算申报开始时间】
            $singleTimeSetting['budgetAuditStart'] || $singleTimeSetting['budgetAuditStart'] = $singleTimeSetting['budgetApplyStart'];
            //单店基金预算审核结束时间默认为【时段结束后15日内】
            $singleTimeSetting['budgetAuditEnd'] || $singleTimeSetting['budgetAuditEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +15 day'));
            //单店基金结算申报开始时间默认为【时段开始时间】
            $singleTimeSetting['settleApplyStart'] || $singleTimeSetting['settleApplyStart'] = $data['startTime'];
            //单店基金结算申报结束时间默认为【时段结束后10日内】
            $singleTimeSetting['settleApplyEnd'] || $singleTimeSetting['settleApplyEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +10 day'));
            //单店基金结算审核开始时间默认为【时段开始时间】
            $singleTimeSetting['settleAuditStart'] || $singleTimeSetting['settleAuditStart'] = $data['startTime'];
            //单店基金结算审核结束时间默认为【时段结束后20天内】  
            $singleTimeSetting['settleAuditEnd'] || $singleTimeSetting['settleAuditEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +20 day'));
            //单店基金结算评分开始时间默认为【时段开始时间】
            $singleTimeSetting['settleScoreStart'] || $singleTimeSetting['settleScoreStart'] = $data['startTime'];
            //单店基金结算评分开始时间默认为【时段结束后20天内】
            $singleTimeSetting['settleScoreEnd'] || $singleTimeSetting['settleScoreEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +20 day'));
            //单店基金金额修改开始时间
            $singleTimeSetting['settleChangeStart'] || $singleTimeSetting['settleChangeStart'] = $data['startTime'];
            //单店基金金额修改结束时间
            $singleTimeSetting['settleChangeEnd'] || $singleTimeSetting['settleChangeEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +30 day'));
            //转换为json格式存进数据库
            $data['singleTimeSetting'] = serialize($singleTimeSetting);
            /**
             * 城市提升基金
             */
            //城市提升基金预算申报开始时间默认为【时段开始前10天】
            $cityTimeSetting['budgetApplyStart'] || $cityTimeSetting['budgetApplyStart'] = date("Y-m-d H:i:s", strtotime($data['startTime'].' -10 day'));
            //城市提升基金预算申报结束时间默认为【时段结束时间】
            $cityTimeSetting['budgetApplyEnd'] || $cityTimeSetting['budgetApplyEnd'] = $data['endTime'];
            //城市提升基金预算审核开始时间默认为【时段开始时间】（城市提升基金没有预算审核）
            $cityTimeSetting['budgetAuditStart'] || $cityTimeSetting['budgetAuditStart'] = $data['startTime'];
            //城市提升基金预算审核结束时间默认为【时段结束时间】（城市提升基金没有预算审核）
            $cityTimeSetting['budgetAuditEnd'] || $cityTimeSetting['budgetAuditEnd'] = $data['endTime'];
            //城市提升基金结算申报开始时间默认为【时段开始时间】
            $cityTimeSetting['settleApplyStart'] || $cityTimeSetting['settleApplyStart'] = $data['startTime'];
            //城市提升基金结算申报结束时间默认为【时段结束后10天内】
            $cityTimeSetting['settleApplyEnd'] || $cityTimeSetting['settleApplyEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +10 day'));
            //城市提升基金结算审核开始时间默认为【时段开始时间】
            $cityTimeSetting['settleAuditStart'] || $cityTimeSetting['settleAuditStart'] = $data['startTime'];
            //城市提升基金结算审核结束时间默认为【时段结束后20天内】
            $cityTimeSetting['settleAuditEnd'] || $cityTimeSetting['settleAuditEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +20 day'));
            //城市提升基金评分开始时间默认为【时段开始时间】
            $cityTimeSetting['settleScoreStart'] || $cityTimeSetting['settleScoreStart'] = $data['startTime'];
            //城市提升基金评分结束时间默认为【时段结束后20天内】
            $cityTimeSetting['settleScoreEnd'] || $cityTimeSetting['settleScoreEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +20 day'));
            //城市提升基金金额修改开始时间
            $cityTimeSetting['settleChangeStart'] || $cityTimeSetting['settleChangeStart'] = $data['startTime'];
            //城市提升基金金额修改结束时间
            $cityTimeSetting['settleChangeEnd'] || $cityTimeSetting['settleChangeEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +30 day'));
            //转换为json格式存进数据库
            $data['cityTimeSetting'] = serialize($cityTimeSetting);
            /**
             * 车展基金时间设置
             */
            //车展基金预算申请开始时间默认为【时段开始前十天】
            $carTimeSetting['budgetApplyStart'] || $carTimeSetting['budgetApplyStart'] = date("Y-m-d H:i:s", strtotime($data['startTime'].' -10 day'));
            //车展基金预算申请结束时间默认为【时段结束时间】
            $carTimeSetting['budgetApplyEnd'] || $carTimeSetting['budgetApplyEnd'] = $data['endTime'];
            //车展基金预算审核开始时间默认为【预算申报开始时间】
            $carTimeSetting['budgetAuditStart'] || $carTimeSetting['budgetAuditStart'] = $carTimeSetting['budgetApplyStart'];
            //车展基金预算审核结束时间默认为【时段结束后15日内】
            $carTimeSetting['budgetAuditEnd'] || $carTimeSetting['budgetAuditEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +15 day'));
            //车展基金结算申报开始时间默认为【时段开始时间】
            $carTimeSetting['settleApplyStart'] || $carTimeSetting['settleApplyStart'] = $data['startTime'];
            //车展基金结算申报结束时间默认为【时段结束后10日内】
            $carTimeSetting['settleApplyEnd'] || $carTimeSetting['settleApplyEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +10 day'));
            //车展基金结算审核开始时间默认为【时段开始时间】
            $carTimeSetting['settleAuditStart'] || $carTimeSetting['settleAuditStart'] = $data['startTime'];
            //车展基金结算审核结束时间默认为【时段结束后20天内】
            $carTimeSetting['settleAuditEnd'] || $carTimeSetting['settleAuditEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +20 day'));
            //车展基金金额修改开始时间
            $carTimeSetting['settleChangeStart'] || $carTimeSetting['settleChangeStart'] = $data['startTime'];
            //车展基金金额修改结束时间
            $carTimeSetting['settleChangeEnd'] || $carTimeSetting['settleChangeEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +30 day'));
            $data['carTimeSetting'] = serialize($carTimeSetting);
            /**
             * 巡展基金时间
             */
            //城市提升基金结算审核开始时间默认为【时段开始时间】
            $xunTimeSetting['settleAuditStart'] || $xunTimeSetting['settleAuditStart'] = $data['startTime'];
            //城市提升基金结算审核结束时间默认为【时段结束后20天内】
            $xunTimeSetting['settleAuditEnd'] || $xunTimeSetting['settleAuditEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +20 day'));
            $data['xunTimeSetting'] = serialize($xunTimeSetting);
            /**
             * 固定传播基金时间设置
             */
            //固定传播预算申请开始时间默认为【时段开始前十天】
            $spreadTimeSetting['budgetApplyStart'] || $spreadTimeSetting['budgetApplyStart'] = date("Y-m-d H:i:s", strtotime($data['startTime'].' -10 day'));
            //固定传播预算申请结束时间默认为【时段结束时间】
            $spreadTimeSetting['budgetApplyEnd'] || $spreadTimeSetting['budgetApplyEnd'] = $data['endTime'];
            //固定传播预算审核开始时间默认为【预算申报开始时间】
            $spreadTimeSetting['budgetAuditStart'] || $spreadTimeSetting['budgetAuditStart'] = $spreadTimeSetting['budgetApplyStart'];
            //固定传播预算审核结束时间默认为【时段结束后15日内】
            $spreadTimeSetting['budgetAuditEnd'] || $spreadTimeSetting['budgetAuditEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +15 day'));
            //固定传播结算申报开始时间默认为【时段开始时间】
            $spreadTimeSetting['settleApplyStart'] || $spreadTimeSetting['settleApplyStart'] = $data['startTime'];
            //固定传播结算申报结束时间默认为【时段结束后10日内】
            $spreadTimeSetting['settleApplyEnd'] || $spreadTimeSetting['settleApplyEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +10 day'));
            //固定传播结算审核开始时间默认为【时段开始时间】
            $spreadTimeSetting['settleAuditStart'] || $spreadTimeSetting['settleAuditStart'] = $data['startTime'];
            //固定传播结算审核结束时间默认为【时段结束后20天内】
            $spreadTimeSetting['settleAuditEnd'] || $spreadTimeSetting['settleAuditEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +20 day'));
            //固定传播金额修改开始时间
            $spreadTimeSetting['settleChangeStart'] || $spreadTimeSetting['settleChangeStart'] = $data['startTime'];
            //固定传播金额修改结束时间
            $spreadTimeSetting['settleChangeEnd'] || $spreadTimeSetting['settleChangeEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +30 day'));
            //$data['spreadTimeSetting'] = serialize($spreadTimeSetting);
            /**
             * 专项单店基金时间设置
             */
            //专项单店基金预算申请开始时间默认为【时段开始前十天】
            $specialTimeSetting['budgetApplyStart'] || $specialTimeSetting['budgetApplyStart'] = date("Y-m-d H:i:s", strtotime($data['startTime'].' -10 day'));
            //专项单店基金预算申请结束时间默认为【时段结束时间】
            $specialTimeSetting['budgetApplyEnd'] || $specialTimeSetting['budgetApplyEnd'] = $data['endTime'];
            //专项单店基金预算审核开始时间默认为【预算申报开始时间】
            $specialTimeSetting['budgetAuditStart'] || $specialTimeSetting['budgetAuditStart'] = $specialTimeSetting['budgetApplyStart'];
            //专项单店基金预算审核结束时间默认为【时段结束后15日内】
            $specialTimeSetting['budgetAuditEnd'] || $specialTimeSetting['budgetAuditEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +15 day'));
            //专项单店基金结算申报开始时间默认为【时段开始时间】
            $specialTimeSetting['settleApplyStart'] || $specialTimeSetting['settleApplyStart'] = $data['startTime'];
            //专项单店基金结算申报结束时间默认为【时段结束后10日内】
            $specialTimeSetting['settleApplyEnd'] || $specialTimeSetting['settleApplyEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +10 day'));
            //专项单店基金结算审核开始时间默认为【时段开始时间】
            $specialTimeSetting['settleAuditStart'] || $specialTimeSetting['settleAuditStart'] = $data['startTime'];
            //专项单店基金结算审核结束时间默认为【时段结束后20天内】
            $specialTimeSetting['settleAuditEnd'] || $specialTimeSetting['settleAuditEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +20 day'));
            //专项单店基金结算评分开始时间默认为【时段开始时间】
            $specialTimeSetting['settleScoreStart'] || $specialTimeSetting['settleScoreStart'] = $data['startTime'];
            //专项单店基金结算评分开始时间默认为【时段结束后20天内】
            $specialTimeSetting['settleScoreEnd'] || $specialTimeSetting['settleScoreEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +20 day'));
            //专项单店基金金额修改开始时间
            $specialTimeSetting['settleChangeStart'] || $specialTimeSetting['settleChangeStart'] = $data['startTime'];
            //专项单店基金金额修改结束时间
            $specialTimeSetting['settleChangeEnd'] || $specialTimeSetting['settleChangeEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +30 day'));
            $data['specialTimeSetting'] = serialize($specialTimeSetting);
            /**
             * 专项城市基金时间设置
             */
            //专项城市基金预算申请开始时间默认为【时段开始前十天】
            $specityTimeSetting['budgetApplyStart'] || $specityTimeSetting['budgetApplyStart'] = date("Y-m-d H:i:s", strtotime($data['startTime'].' -10 day'));
            //专项城市基金预算申请结束时间默认为【时段结束时间】
            $specityTimeSetting['budgetApplyEnd'] || $specityTimeSetting['budgetApplyEnd'] = $data['endTime'];
            //专项城市基金预算审核开始时间默认为【预算申报开始时间】
            $specityTimeSetting['budgetAuditStart'] || $specityTimeSetting['budgetAuditStart'] = $specityTimeSetting['budgetApplyStart'];
            //专项城市基金预算审核结束时间默认为【时段结束后15日内】
            $specityTimeSetting['budgetAuditEnd'] || $specityTimeSetting['budgetAuditEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +15 day'));
            //专项城市基金结算申报开始时间默认为【时段开始时间】
            $specityTimeSetting['settleApplyStart'] || $specityTimeSetting['settleApplyStart'] = $data['startTime'];
            //专项城市基金结算申报结束时间默认为【时段结束后10日内】
            $specityTimeSetting['settleApplyEnd'] || $specityTimeSetting['settleApplyEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +10 day'));
            //专项城市基金结算审核开始时间默认为【时段开始时间】
            $specityTimeSetting['settleAuditStart'] || $specityTimeSetting['settleAuditStart'] = $data['startTime'];
            //专项城市基金结算审核结束时间默认为【时段结束后20天内】
            $specityTimeSetting['settleAuditEnd'] || $specityTimeSetting['settleAuditEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +20 day'));
            //专项城市基金结算评分开始时间默认为【时段开始时间】
            $specityTimeSetting['settleScoreStart'] || $specityTimeSetting['settleScoreStart'] = $data['startTime'];
            //专项城市基金结算评分开始时间默认为【时段结束后20天内】
            $specityTimeSetting['settleScoreEnd'] || $specityTimeSetting['settleScoreEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +20 day'));
            //专项城市基金金额修改开始时间
            $specityTimeSetting['settleChangeStart'] || $specityTimeSetting['settleChangeStart'] = $data['startTime'];
            //专项城市基金金额修改结束时间
            $specityTimeSetting['settleChangeEnd'] || $specityTimeSetting['settleChangeEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +30 day'));
            $data['specityTimeSetting'] = serialize($specityTimeSetting);
            if ($id){
                $timeId = $this->timeUnitModel->edit($id, $data);
            }else{
                $timeId = $this->timeUnitModel->add($data);
            }
            if ($timeId){
                $this->timeUnitModel->createTimeUnitCache();
                showSuccess('时段保存成功');
            }else{
                showError('时段保存失败');
            }
        }else{
            $id = (int)$this->input->get('id');
            if ($id){
                $this->viewData['timeUnitData'] = $this->timeUnitModel->getNewData($id);
                $this->viewData['timeUnitData'] || showError('数据不存在');
            }
            $this->load->view('Admin/timeUnitAdd', $this->viewData);
        }
    }
    /**
     * 巡展手动同步
     */
    function xunSync(){
        //设置超时时间
        set_time_limit(0);
        //数据配置
        $dataTypeCon = array(1=>'预算同步-活动项目',2=>'预算同步-线上广宣',3=>'预算同步-线下活动',4=>'结算同步-活动项目',5=>'结算同步-线上广宣',6=>'结算同步-线下活动');
        $this->viewData['dataTypeCon'] = $dataTypeCon;
        //条件
        $where = array();
        //$where['dataType'] = (int)$_REQUEST['dataTypa'];
        //$where['dataType'] || $where['dataType'] = 1;
        $where['year'] = (int)$_REQUEST['year'];
        $where['year'] || $where['year'] = $this->config->item('nowYear');
        $where['month'] = (int)$_REQUEST['month'];
        $where['month'] || $where['month'] = date("m");
        $where['updateNum'] = (int)$_REQUEST['updateNum'];
        $where['updateNum'] || $where['updateNum'] = 20;
        $where['updatePage'] = (int)$_REQUEST['updatePage'];
        $where['updatePage'] || $where['updatePage'] = 1;
        $where['state'] = (int)$_REQUEST['state'];
        //是否执行
        $runing = (int)$this->input->get('runing');
        if ($runing == 1){
            //加载巡展同步类
            $this->load->library('XunSync', NULL, 'XunSync');
            //switch ($where['dataType']){
                //case 1://项目预算同步
                    $beginNumber = 1;
                    do{
                        $rs = $this->XunSync->budgetProjectSync($where['year'], $where['month'], $where['updateNum'], $beginNumber, '08', $where['state']);
                        $codeStr = $rs['code']==1?'<font color="green">成功</font>':'<font color="red">失败</font>';
                        $log = '更新项目预算情况（[年份：'.$where['year'].'],[月份:'.$where['month'].'],[更新状态：'.$codeStr.'],[更新反馈：'.$rs['msg'].'],[更新数量：'.(isset($rs['data']['updateCount']) ? $rs['data']['updateCount'] : 0).']）<br>';
                        echo $log;
                        //记录错误日志
                        if ($rs['code'] == 0){
                            $this->xunSyncRecord('项目预算同步错误', $log);
                        }
                        echo str_repeat(" ",4096);
                        //usleep(500);
                        $updateCount = isset($rs['data']['total']) ? $rs['data']['total'] : 0;
                        $where['state'] == 1 && $beginNumber ++;
                    }while ( $updateCount == $where['updateNum']);
                    //break;
                //case 2://广宣预算同步
                    $beginNumber = 1;
                    do{
                        $rs = $this->XunSync->budgetGxSync($where['year'], $where['month'], $where['updateNum'], $beginNumber, '08', $where['state']);
                        $codeStr = $rs['code']==1?'<font color="green">成功</font>':'<font color="red">失败</font>';
                        $log = '更新广宣预算情况（[年份：'.$where['year'].'],[月份:'.$where['month'].'],[更新状态：'.$codeStr.'],[更新反馈：'.$rs['msg'].'],[更新数量：'.(isset($rs['data']['updateCount']) ? $rs['data']['updateCount'] : 0).']）<br>';
                        echo $log;
                        //记录错误日志
                        if ($rs['code'] == 0){
                            $this->xunSyncRecord('广宣预算同步错误', $log);
                        }
                        echo str_repeat(" ",4096);
                        //usleep(500);
                        $updateCount = isset($rs['data']['total']) ? $rs['data']['total'] : 0;
                        $where['state'] == 1 && $beginNumber ++;
                    }while ( $updateCount == $where['updateNum']);
                    //break;
                //case 3://线下活动预算同步
                    $beginNumber = 1;
                    do{
                        $rs = $this->XunSync->budgetOfflineSync($where['year'], $where['month'], $where['updateNum'], $beginNumber, '08', $where['state']);
                        $codeStr = $rs['code']==1?'<font color="green">成功</font>':'<font color="red">失败</font>';
                        $log ='更新线下活动预算情况（[年份：'.$where['year'].'],[月份:'.$where['month'].'],[更新状态：'.$codeStr.'],[更新反馈：'.$rs['msg'].'],[更新数量：'.(isset($rs['data']['updateCount']) ? $rs['data']['updateCount'] : 0).']）<br>';
                        echo $log;
                        //记录错误日志
                        if ($rs['code'] == 0){
                            $this->xunSyncRecord('线下活动预算同步错误', $log);
                        }
                        echo str_repeat(" ",4096);
                        //usleep(500);
                        $updateCount = isset($rs['data']['total']) ? $rs['data']['total'] : 0;
                        $where['state'] == 1 && $beginNumber ++;
                    }while ( $updateCount == $where['updateNum']);
                    //break;
                //case 4://项目结算同步
                    $beginNumber = 1;
                    do{
                        $rs = $this->XunSync->settleProjectSync($where['year'], $where['month'], $where['updateNum'], $beginNumber, '08', $where['state']);
                        $codeStr = $rs['code']==1?'<font color="green">成功</font>':'<font color="red">失败</font>';
                        $log ='更新项目结算情况（[年份：'.$where['year'].'],[月份:'.$where['month'].'],[更新状态：'.$codeStr.'],[更新反馈：'.$rs['msg'].'],[更新数量：'.(isset($rs['data']['updateCount']) ? $rs['data']['updateCount'] : 0).']）<br>';
                        echo $log;
                        //记录错误日志
                        if ($rs['code'] == 0){
                            $this->xunSyncRecord('项目结算同步错误', $log);
                        }
                        echo str_repeat(" ",4096);
                        //usleep(500);
                        $updateCount = isset($rs['data']['total']) ? $rs['data']['total'] : 0;
                        $where['state'] == 1 && $beginNumber ++;
                    }while ( $updateCount == $where['updateNum']);
                    //break;
                //case 5://广宣结算同步
                    $beginNumber = 1;
                    do{
                        $rs = $this->XunSync->settleGxSync($where['year'], $where['month'], $where['updateNum'], $beginNumber, '08', $where['state']);
                        $codeStr = $rs['code']==1?'<font color="green">成功</font>':'<font color="red">失败</font>';
                        $log ='更新广宣结算情况（[年份：'.$where['year'].'],[月份:'.$where['month'].'],[更新状态：'.$codeStr.'],[更新反馈：'.$rs['msg'].'],[更新数量：'.(isset($rs['data']['updateCount']) ? $rs['data']['updateCount'] : 0).']）<br>';
                        echo $log;
                        //记录错误日志
                        if ($rs['code'] == 0){
                            $this->xunSyncRecord('广宣结算同步错误', $log);
                        }
                        echo str_repeat(" ",4096);
                        //usleep(500);
                        $updateCount = isset($rs['data']['total']) ? $rs['data']['total'] : 0;
                        $where['state'] == 1 && $beginNumber ++;
                    }while ( $updateCount == $where['updateNum']);
                    //break;
                //case 6://线下活动结算同步
                    $beginNumber = 1;
                    do{
                        $rs = $this->XunSync->settleOfflineSync($where['year'], $where['month'], $where['updateNum'], $beginNumber, '08', $where['state']);
                        $codeStr = $rs['code']==1?'<font color="green">成功</font>':'<font color="red">失败</font>';
                        $log ='更新线下活动结算情况（[年份：'.$where['year'].'],[月份:'.$where['month'].'],[更新状态：'.$codeStr.'],[更新反馈：'.$rs['msg'].'],[更新数量：'.(isset($rs['data']['updateCount']) ? $rs['data']['updateCount'] : 0).']）<br>';
                        echo $log;
                        //记录错误日志
                        if ($rs['code'] == 0){
                            $this->xunSyncRecord('线下活动结算同步错误', $log);
                        }
                        echo str_repeat(" ",4096);
                        usleep(500);
                        $updateCount = isset($rs['data']['total']) ? $rs['data']['total'] : 0;
                        $where['state'] == 1 && $beginNumber ++;
                    }while ( $updateCount == $where['updateNum']);
                    //break;
            //}
        }else{
            $this->viewData['where'] = $where;
            $this->load->view('Admin/xunSync', $this->viewData);
        }
    }
    /**
     * 记录巡展同步日志
     * @param string $typeName
     * @param string $log
     */
    function xunSyncRecord($typeName, $log){
        $this->load->model('xLogModel');
        $this->xLogModel->add(array('typeName'=>$typeName,'log'=>$log,'createTime'=>date("Y-m-d H:i:s")));
        return true;
        //邮件提醒
        $this->load->library('email');
        //发件人
        $this->email->from('zhaojianhui129@163.com', '巡展同步邮件警报');
        //收件人
        $this->email->to('zhaojianhui129@vip.qq.com');
        //抄送
        $this->email->cc(array('ivy.huang@bluedoorindex.com'));
        //暗送
        //$this->email->bcc('them@their-example.com');
        
        $this->email->subject($typeName);
        $this->email->message($log);
        
        $this->email->send();
        //echo $this->email->print_debugger();
    }
    /**
     * 巡展数据日志查看
     * @return [type] [description]
     */
    function xunLogDataList(){
        $this->load->model('xDataLogModel');
        //筛选条件
        $where = array('id > '=> 0);
        //数据类型
        $type = (int)$this->input->get('type');
        $this->viewData['type'] = $type;
        //订单号
        $orderId = $this->input->get('orderId');
        $this->viewData['orderId'] = $orderId;
        $orderId && $where['orderId'] = $orderId;
        //活动ID
        $activityId = $this->input->get('activityId');
        $this->viewData['activityId'] = $activityId;
        $activityId && $where['activityId'] = $activityId;
        //流水号
        $costNo = $this->input->get('costNo');
        $this->viewData['costNo'] = $costNo;
        $costNo && $where['costNo'] = $costNo;
        $count = $this->xDataLogModel->getCount($where);
        $type && $where['type'] = $type;
        //分页
        $this->load->library('pagination');
        $getParam = $_GET;
        unset($getParam['c'], $getParam['m'], $getParam['page']);
        $config['base_url'] = printUrl('Admin', 'xunLogDataList', $getParam);
        $config['total_rows'] = $count;
        $this->pagination->initialize($config);
        $this->viewData['pager'] = $this->pagination->create_links();
        $page = (int)$this->input->get('page');
        $limit = $this->pagination->per_page;
        //分页数据
        $this->viewData['list'] = $this->xDataLogModel->getNewList($where, $limit, $page, '', 'id desc');
        //数据类型
        $this->viewData['typeList'] = $this->xDataLogModel->getTypeList();
        $this->load->view('Admin/xunLogDataList', $this->viewData);
    }
    /**
     * 巡展错误日志查看
     * @return [type] [description]
     */
    function xunLogList(){
        $this->load->model('xLogModel');
        $where = array('id > '=> 0);
        $count = $this->xLogModel->getCount($where);
        //分页
        $this->load->library('pagination');
        $getParam = $_GET;
        unset($getParam['c'], $getParam['m'], $getParam['page']);
        $config['base_url'] = printUrl('Admin', 'xunLogList', $getParam);
        $config['total_rows'] = $count;
        $this->pagination->initialize($config);
        $this->viewData['pager'] = $this->pagination->create_links();
        $page = (int)$this->input->get('page');
        $limit = $this->pagination->per_page;
        //分页数据
        $this->viewData['list'] = $this->xLogModel->getList($where, $limit, $page, '', 'id desc');
        $this->load->view('Admin/xunLogList', $this->viewData);
    }
    /**
     * 巡展订单数据导出
     */
    function xunOutput(){
        $runing = (int)$this->input->get('runing');
        if ($runing == 1){
            //查询条件
            $where = array();
            //专营店筛选
            $area = $this->input->get('area');
            $this->viewData['area'] = $area;
            $region = $this->input->get('region');
            $this->viewData['region'] = $region;
            $province = $this->input->get('province');
            $this->viewData['province'] = $province;
            $city = $this->input->get('city');
            $this->viewData['city'] = $city;
            $storeId = $this->input->get('storeId');
            $this->viewData['storeId'] = $storeId;
            $keyword = $this->input->get('keyword');
            $this->viewData['keyword'] = $keyword;
            //查找对应地区列表
            $placeStoreList = $this->sellpointModel->searchStore($area, $region, '', $province, $city, $storeId, $keyword);
            $placeStoreIds = array_keys($placeStoreList);
            $this->viewData['allStoreList'] = $placeStoreList;
            $where['storeId'] = array('in', $placeStoreIds);
            //年份
            $year = $this->input->get('year');
            $year || $year = $this->config->item('nowYear');
            $this->viewData['year'] = $year;
            //时间单位
            $timeUnit = (int)$this->input->get('timeUnit');
            $this->viewData['timeUnit'] = $timeUnit;
            $this->viewData['timeUnit'] && $where['timeUnit'] = $timeUnit;
            //年份条件
            $where['year'] = $year;
            $this->load->model('xProjectModel');
            $this->viewData['list'] = $this->xProjectModel->getNewList($where, NULL, NULL, '', 'storeId ASC');
            
            $fileName = 'RMP巡展所获取的所有订单号'.$year.'年';
            
            $timeUnitList = $this->timeUnitModel->getCacheData('timeUnitList');
            isset($timeUnitList[$year][$timeUnit]) && $fileName .= '-'.$timeUnitList[$year][$timeUnit];
            
            //设置excel文件下载头
            setExcelDownHeader($fileName);
            
            $this->load->view('Admin/xunOutputExcel', $this->viewData);
        }else{
            $this->load->view('Admin/xunOutput', $this->viewData);
        }
    }
    function xunDataCheck(){
        if ($_POST){
            $year = (int)$this->input->post('year');
            $year || showError('请选择年份');
            $timeUnit = (int)$this->input->post('timeUnit');
            $timeUnit || showError('请选择时段');
            //上传文件
            $fileData = $this->upload();
            $fullPath = $fileData['full_path'];
            $handle = fopen ($fullPath, "r");
            $i = 0; $ok = 0; $err = 0;
            $successOrderIds = array();//同步成功的订单号列表
            $errorOrderIds = array();//同步成功的订单号列表
            $reportList = array();//array('orderId'=>'','msg'=>'','updateTime'=>'')
            //载入单店主线流程模型
            $this->load->model('xDataLogModel');
            while ($item = fgetcsv($handle, 10000, ",")){
                if($i==0){$i++;continue;}//跳过第一行
                $orderId = trim($item[0]);
                $findLogData = $this->xDataLogModel->getNewData(array('orderId'=>$orderId,'type'=>4), '', 'id desc');
                
                if ($findLogData){
                    if ($findLogData['isSync'] == 1){
                        $reportList[] = array('orderId'=>$orderId,'msg'=>'<font color="green">同步成功</font>','updateTime'=>$findLogData['createTime']);
                        $successOrderIds[] = $orderId;
                    }else{
                        $reportList[] = array('orderId'=>$orderId,'msg'=>'<font color="red">数据无效，已经将数据过滤，可能原因是巡展类型标志非1或者活动类型为车展外场，</font>'.$findLogData['dataStr'],'updateTime'=>$findLogData['createTime']);
                        $errorOrderIds[] = $orderId;
                    }
                }else{
                    $reportList[] = array('orderId'=>$orderId,'msg'=>'<font color="red">RMP未接收到数据</font>','updateTime'=>'');
                    $errorOrderIds[] = $orderId;
                }
                usleep(500);
            }
            //查找出是否是多同步过来的订单
            $this->load->model('xProjectModel');
            $errorProjectList = $this->xProjectModel->getList(array('year'=>$year,'timeUnit'=>$timeUnit,'orderId'=>array('notIn', array_merge($successOrderIds, $errorOrderIds))));
            foreach ($errorProjectList as $v){
                $reportList[] = array('orderId'=>$v['orderId'],'msg'=>'<font color="red">同步多余的订单</font>','updateTime'=>date("Y-m-d H:i:s", $v['updateTime']));
                $errorOrderIds[] = $v['orderId'];
            }
            $reportList[] = array('orderId'=>'汇总','msg'=>'同步成功总数：'.count($successOrderIds).',同步有误总数:'.count($errorOrderIds));
            $this->viewData['reportList'] = $reportList;
            $fileName = 'BTL巡展数据检查情况';
            //设置excel文件下载头
            setExcelDownHeader($fileName);
            
            $this->load->view('Admin/xunDataCheckExcel', $this->viewData);
        }else{
            //年
            $year = $this->input->get('year');
            $year || $year = $this->config->item('nowYear');
            $this->viewData['year'] = $year;
            $this->load->view('Admin/xunDataCheck', $this->viewData);
        }
    }
    /**
     * 数据重建
     */
    function rebuild(){
        $runing = (int)$this->input->get('runing');
        if ($runing == 1){
            $dataType = $this->input->get('dataType');
            $year = (int)$this->input->get('year');
            $timeUnit = (int)$this->input->get('timeUnit');
            $page = (int)$this->input->get('page');
            $page || $page = 1;
            $pageSize = 50;
            //条件ID
            $idsData = $this->input->get('ids');
            $ids = array();
            if ($idsData){
                $idsData = str_replace('，', ',', $idsData);
                if (strpos($idsData, ',') !== false){
                    $idsData = explode(',', $idsData);
                    foreach ($idsData as $v){
                        $ids[] = (int)$v;
                    }
                }else{
                    $ids[] = (int)$idsData;
                }
            }
            $where = array('year'=>$year,'timeUnit'=>$timeUnit);
            if ((int)$dataType[0] == 1){//单店基金
                if((int)$dataType == 11 && $ids){
                    $where['id'] = array('in', $ids);
                }
                $this->load->model('singleProcessModel');
                $procList = $this->singleProcessModel->getList($where);
            }elseif ((int)$dataType[0] == 2){//城市基金
                if((int)$dataType == 21 && $ids){
                    $where['id'] = array('in', $ids);
                }
                $this->load->model('cityProcessModel');
                $procList = $this->cityProcessModel->getList($where);
            }elseif ((int)$dataType[0] == 3){//车展基金
                if ((int)$dataType == 31 && $ids){
                    $where['id'] = array('in', $ids);
                }
                $this->load->model('carProcessModel');
                $procList = $this->carProcessModel->getList($where);
            }elseif ((int)$dataType[0] == 4){//巡展基金
                if ((int)$dataType == 41 && $ids){
                    $where['id'] = array('in', $ids);
                }
                $this->load->model('xunProcessModel');
                $procList = $this->xunProcessModel->getList($where);
            }elseif ((int)$dataType[0] == 5){//固定传播基金
                unset($where['timeUnit']);
                if ((int)$dataType == 51 && $ids){
                    $where['id'] = array('in', $ids);
                }
                $this->load->model('spreadProcessModel');
                $procList = $this->spreadProcessModel->getList($where);
            }elseif ((int)$dataType[0] == 6){//专项单店基金
                if ((int)$dataType == 61 && $ids){
                    $where['id'] = array('in', $ids);
                }
                $this->load->model('specialProcessModel');
                $procList = $this->specialProcessModel->getList($where);
            }elseif ((int)$dataType[0] == 7){//专项城市基金
                if ((int)$dataType == 71 && $ids){
                    $where['id'] = array('in', $ids);
                }
                $this->load->model('specialProcessModel');
                $procList = $this->specialProcessModel->getList($where);
            }
            $procIds = array_keys($procList);
            $childWhere = array('procId'=>array('in',$procIds));
            $ids && $childWhere['id'] = array('in', $ids);
            $count = 0;
            switch ((int)$dataType){
                case 11://单店基金汇总金额
                    foreach ($procIds as $v){
                        $this->singleProcessModel->updateMoney($v);
                        echo '更新[单店基金汇总金额]:ID为：'.$v.'的金额成功<br>';
                        echo str_repeat(" ",4096);
                    }
                    break;
                case 12://单店基金-项目活动金额
                    $this->load->model('singleProjectModel');
                    $list = $this->singleProjectModel->getList($childWhere, $pageSize, ($page-1)*$pageSize,'', 'id asc');
                    foreach ($list as $k => $v){
                        $this->singleProjectModel->updateMoney($k);
                        echo '更新[单店基金-项目活动金额]:ID为：'.$k.'的金额成功<br>';
                        echo str_repeat(" ",4096);
                    }
                    $count = count($list);
                    break;
                case 13: //单店基金-天网行动
                    $this->load->model('singleNetModel');
                    $list = $this->singleNetModel->getList($childWhere, $pageSize, ($page-1)*$pageSize,'', 'id asc');
                    foreach ($list as $k => $v){
                        $this->singleNetModel->updateMoney($k);
                        echo '更新[单店基金-天网行动]:ID为：'.$k.'的金额成功<br>';
                        echo str_repeat(" ",4096);
                    }
                    $count = count($list);
                    break;
                case 21: //城市提升基金活动汇总金额
                    foreach ($procIds as $v){
                        $this->cityProcessModel->updateMoney($v);
                        echo '更新[城市提升基金活动汇总金额]:ID为：'.$v.'的金额成功<br>';
                        echo str_repeat(" ",4096);
                    }
                    break;
                case 22: //城市提升基金-城市提升基金项目
                    $this->load->model('cityProjectModel');
                    $list = $this->cityProjectModel->getList($childWhere, $pageSize, ($page-1)*$pageSize,'', 'id asc');
                    foreach ($list as $k => $v){
                        $this->cityProjectModel->updateMoney($k);
                        echo '更新[城市提升基金-城市提升基金项目]:ID为：'.$k.'的金额成功<br>';
                        echo str_repeat(" ",4096);
                    }
                    $count = count($list);
                    break;
                case 31: //车展基金-汇总金额
                    foreach ($procIds as $k => $v){
                        $this->carProcessModel->updateMoney($v);
                        echo '更新[车展基金-汇总金额]:ID为：'.$v.'的金额成功<br>';
                        echo str_repeat(" ",4096);
                    }
                    $count = count($list);
                    break;
                case 32: //车展基金-地方车展
                    $this->load->model('carProjectModel');
                    $list = $this->carProjectModel->getList($childWhere, $pageSize, ($page-1)*$pageSize,'', 'id asc');
                    foreach ($list as $k => $v){
                        $this->carProjectModel->updateMoney($k);
                        echo '更新[车展基金-地方车展]:ID为：'.$k.'的金额成功<br>';
                        echo str_repeat(" ",4096);
                    }
                    $count = count($list);
                    break;
                case 33://车展基金-车展广宣
                    $this->load->model('carGxModel');
                    $list = $this->carGxModel->getList($childWhere, $pageSize, ($page-1)*$pageSize,'', 'id asc');
                    foreach ($list as $k => $v){
                        $this->carGxModel->updateMoney($k);
                        echo '更新[车展基金-车展广宣:ID为：'.$k.'的金额成功<br>';
                        echo str_repeat(" ",4096);
                    }
                    $count = count($list);
                    break;
                case 34://车展基金-车展场租
                    $this->load->model('carRentModel');
                    $list = $this->carRentModel->getList($childWhere, $pageSize, ($page-1)*$pageSize,'', 'id asc');
                    foreach ($list as $k => $v){
                        $this->carRentModel->updateMoney($k);
                        echo '更新[车展基金-车展场租:ID为：'.$k.'的金额成功<br>';
                        echo str_repeat(" ",4096);
                    }
                    $count = count($list);
                    break;
                case 35://车展基金-车展配套
                    $this->load->model('carMatchingModel');
                    $list = $this->carMatchingModel->getList($childWhere, $pageSize, ($page-1)*$pageSize,'', 'id asc');
                    foreach ($list as $k => $v){
                        $this->carMatchingModel->updateMoney($k);
                        echo '更新[车展基金-车展配套:ID为：'.$k.'的金额成功<br>';
                        echo str_repeat(" ",4096);
                    }
                    $count = count($list);
                    break;
                case 41://巡展-项目金额重建
                    $this->load->model('xProjectModel');
                    $list = $this->xProjectModel->getList(array('year'=>$year,'timeUnit'=>$timeUnit), $pageSize, ($page-1)*$pageSize, 'activityId', 'createTime asc');
                    foreach ($list as $k => $v){
                        $this->xProjectModel->updateMoney($v['activityId']);
                        echo '更新[巡展-项目金额]:活动项目ID为：'.$v['activityId'].'的金额成功<br>';
                        echo str_repeat(" ",4096);
                    }
                    $count = count($list);
                    break;
                case 42://巡展-广宣金额重建
                    $this->load->model('xPGxModel');
                    $list = $this->xPGxModel->getList(array('year'=>$year,'timeUnit'=>$timeUnit), $pageSize, ($page-1)*$pageSize, 'costNo', 'createTime asc');
                    foreach ($list as $k => $v){
                        $this->xPGxModel->updateMoney($v['costNo']);
                        echo '更新[巡展-广宣金额]:流水号ID为：'.$v['costNo'].'的金额成功<br>';
                        echo str_repeat(" ",4096);
                    }
                    $count = count($list);
                    break;
                case 43://巡展-线下活动金额重建
                    $this->load->model('xPOfflineModel');
                    $list = $this->xPOfflineModel->getList(array('year'=>$year,'timeUnit'=>$timeUnit), $pageSize, ($page-1)*$pageSize, 'costNo', 'createTime asc');
                    foreach ($list as $k => $v){
                        $this->xPOfflineModel->updateMoney($v['costNo']);
                        echo '更新[巡展-线下活动金额]:流水号ID为：'.$v['costNo'].'的金额成功<br>';
                        echo str_repeat(" ",4096);
                    }
                    $count = count($list);
                    break;
                case 51://固定传播基金汇总金额
                    foreach ($procIds as $k => $v){
                        $this->spreadProcessModel->updateMoney($v);
                        echo '更新[固定传播基金汇总金额]:ID为：'.$v.'的金额成功<br>';
                        echo str_repeat(" ",4096);
                    }
                    $count = count($list);
                    break;
                case 61://专项基金汇总金额
                    foreach ($procIds as $k => $v){
                        $this->specialProcessModel->updateMoney($v);
                        echo '更新[专项基金汇总金额]:ID为：'.$v.'的金额成功<br>';
                        echo str_repeat(" ",4096);
                    }
                    $count = count($list);
                    break;
                case 62: //专项基金-项目活动
                    $this->load->model('specialProjectModel');
                    $list = $this->specialProjectModel->getList($childWhere, $pageSize, ($page-1)*$pageSize,'', 'id asc');
                    foreach ($list as $k => $v){
                        $this->specialProjectModel->updateMoney($k);
                        echo '更新[专项基金-项目活动]:ID为：'.$k.'的金额成功<br>';
                        echo str_repeat(" ",4096);
                    }
                    $count = count($list);
                    break;
                default:
                    showError('无效类别');
            }
            if ($count < $pageSize){
                echo "全部执行完成";
            }else{
                unset($_GET['c'],$_GET['m']);
                $_GET['page'] = $page + 1;
                echo '<script language="javascript" type="text/javascript">
                           window.location.href="'.printUrl('Admin','rebuild', $_GET).'"; 
                    </script>';
            }
            //showSuccess('执行成功,成功计算条数：'.$ok);
        }else{
            $dataTypeCon = array(
                11 => '单店基金汇总金额', 12 => '单店基金-项目活动金额', 13 =>'单店基金-天网行动',
                21 => '城市基金汇总金额', 22 => '城市提升基金-城市提升基金项目',
                31 => '车展基金汇总金额', 32 => '车展基金地方车展重建', 33 => '车展基金车展广宣重建', 34 => '车展基金车展场租重建', 35 => '车展基金车展配套重建',
                41 => '巡展-项目金额重建', 42 => '巡展基金-广宣金额 重建', 43 => '巡展基金-线下活动金额重建',
                51 => '固定传播基金汇总金额',
                61 => '专项基金汇总金额', 62 => '专项基金-项目活动重建',
            );
            $this->viewData['dataTypeCon'] = $dataTypeCon;
            $this->viewData['year'] = $this->config->item('nowYear');
            $this->load->view('Admin/rebuild', $this->viewData);
        }
    }
    /**
     * 查询出项目可结算金额错误
     */
    function errorProject() {
        $query = $this->db->query('SELECT a.* FROM rmp2015_new2_SProject a JOIN rmp2015_new2_SingleProcess b ON a.procId=b.id WHERE b.timeUnit=4 ORDER BY a.storeId ASC;');
        $list = $query->result_array();
        $sellpointIds = array();
        $validIds = array();
        foreach ($list as $v){
            $errCanSettleMoney = min((float)$v['canUseMoney'], (float)$v['settleApply']);
            $okCanSettleMoney = min((float)$v['canUseMoney'], (float)$v['settleAudit']);
            if ($errCanSettleMoney != $okCanSettleMoney){
                $validIds[] = $v['id'];
                in_array($v['storeId'], $sellpointIds) || $sellpointIds[] = (int)$v['storeId'];
            }
        }
        $this->load->model('singleProjectModel');
        $this->viewData['list'] = $this->singleProjectModel->getNewList(array('id'=>array('in',$validIds)));
        $this->load->model('sellpointModel');
        $this->viewData['allStoreList'] = $this->sellpointModel->getNewList(array('SellPointID'=>array('in', $sellpointIds)));
        
        $fileName = '7-8月项目计算错误列表及专营店汇总列表';
        //设置excel文件下载头
        setExcelDownHeader($fileName);
        
        $this->load->view('Admin/errorProject', $this->viewData);
    }
    /**
     * 查询出项目可结算金额错误
     */
    function netList() {
        $timeUnit = 5;
        $timeUnitStr = '9-10';
        $this->viewData['timeUnitStr'] = $timeUnitStr;
        $query = $this->db->query('SELECT b.year,b.timeUnit,a.* FROM rmp2015_new2_SNet a JOIN rmp2015_new2_SingleProcess b ON a.procId=b.id WHERE b.timeUnit='.$timeUnit.' ORDER BY a.storeId ASC;');
        $list = $query->result_array();
        $sellpointIds = array();
        $validIds = array();
        foreach ($list as $v){
            $validIds[] = $v['id'];
            in_array($v['storeId'], $sellpointIds) || $sellpointIds[] = (int)$v['storeId'];
        }
        $this->load->model('singleNetModel');
        $this->viewData['list'] = $this->singleNetModel->getNewList(array('id'=>array('in',$validIds)));
        $this->load->model('sellpointModel');
        $this->viewData['allStoreList'] = $this->sellpointModel->getNewList(array('SellPointID'=>array('in', $sellpointIds)));
    
        $fileName = $timeUnitStr.'月天网行动列表';
        //设置excel文件下载头
        setExcelDownHeader($fileName);
        $this->load->view('Admin/netList', $this->viewData);
    }
    /**
     * 上传文件
     * @return array
     */
    function upload(){
        //允许上传文件格式
        $config['allowed_types'] = 'csv';
        $extPath = date('Ymd');//子目录
        //上传目录
        $config['upload_path'] = '../upload/'. $extPath .'/';
        //网站目录
        $webPath = substr($config['upload_path'], 2);
        //判断上传目录是否存在，不存在则创建
        if (!is_dir($config['upload_path'])){
            mkdir($config['upload_path'],0777);
        }
        //是否重命名
        $config['encrypt_name'] = TRUE;
        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload('inputFile'))
        {
            $error = $this->upload->display_errors('','');
            showError($error);
        } else {
            $data = $this->upload->data();
            return $data;
        }
    }
}